home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 113_01 / a68eval.c < prev    next >
Text File  |  1985-03-09  |  9KB  |  235 lines

  1. /*
  2.     HEADER:        CUG113;
  3.     TITLE:        6800 Cross-Assembler (BDS C Version);
  4.     FILENAME:    A68EVAL.C;
  5.     VERSION:    2.6;
  6.     DATE:        07/22/1985;
  7.  
  8.     DESCRIPTION:    "This program lets you use your CP/M-80-based computer
  9.             to assemble code for the Motorola 6800, 6801, 6802,
  10.             6803, 6808, and 68701 microprocessors.  The program is
  11.             written in BDS C for the best possible performance on
  12.             8-bit machines.  All assembler features are supported
  13.             except relocation, linkage, listing control, and
  14.             macros.";
  15.  
  16.     KEYWORDS:    Software Development, Assemblers, Cross-Assemblers,
  17.             Motorola, MC6800, MC6801;
  18.  
  19.     SEE-ALSO:    CUG149, 6801 Cross-Assembler (Portable);
  20.  
  21.     SYSTEM:        CP/M-80;
  22.     COMPILERS:    BDS C;
  23.  
  24.     WARNINGS:    "This package is specifically tailored to CP/M-80
  25.             machines and the rather non-standard, but high-
  26.             performance BDS C compiler.  For other environments,
  27.             use the portable version of this package on CUG149.";
  28.  
  29.     AUTHORS:    William C. Colley III;
  30. */
  31.  
  32. /*
  33.     6800/6801 Cross-Assembler  v. 2.6
  34.  
  35.     May, 1980
  36.  
  37.     July, 1980 -- Rev. 2.2 consisting of fixing the M errors that
  38.         come from forward references in FDB and FCB pseudo-ops.
  39.  
  40.     October, 1980 -- Rev. 2.3 consisting of updating the assembly
  41.         language file and I/O routines to match and take
  42.         advantage of BDS C V1.4.
  43.  
  44.     October, 1983 -- Rev. 2.4 consisting of adding the CPU pseudo-op,
  45.         adding the 6801 CPU's extra opcodes, and speeding up the
  46.         code a bit.
  47.  
  48.     September, 1984 -- Rev. 2.5 consisting of fixing bugs with the symbol
  49.         table sort, the writing of files to specified drives, and the
  50.         handling of blank input lines.
  51.  
  52.     June, 1985 -- Rev. 2.6 consisting of fixing a bug in the IF block
  53.         nesting mechanism.
  54.  
  55.     Copyright (c) 1980,83,84,85 William C. Colley, III.
  56.  
  57. File:    a68eval.c
  58.  
  59. Routines to crunch on source text chunks and give back
  60. evaluated expressions, opcode parameters, etc.
  61. */
  62.  
  63. /*  Get Globals:  */
  64.  
  65. #include "a68.h"
  66.  
  67. /*
  68. Function to evaluate the next expression on the present source line.
  69. Function returns the value of the expression.  In addition, a global
  70. flag, evalerr, is set to TRUE if an error occurred anywhere in the
  71. evaluation process.  The precidence parameter, prec, is for the benefit
  72. of the recursion.  In the first call to the routine, use START.
  73. */
  74.  
  75. eval(prec)
  76. char prec;
  77. {
  78.     char getitem(), c;
  79.     unsigned valu1, valu2;
  80.  
  81.     for (;;) {
  82.         switch (getitem(&valu1,SMALST)) {
  83.             case REGISTR:
  84.             case IMMED:      evalerr = TRUE;  markerr('S');  break;
  85.  
  86.             case COMMA:      if (prec != START) backitem(0,COMMA);
  87.                           goto enderr;
  88.  
  89.             case END_LIN: backitem(0,END_LIN);
  90.             enderr:       evalerr = TRUE;  markerr('E');  return 0;
  91.  
  92.             case OPERATR: if (valu1 == NOT) valu1 = ~eval(UOP2);
  93.                           else if (valu1 == '-') valu1 = -eval(UOP1);
  94.                           else if (valu1 == '+') valu1 = eval(UOP1);
  95.                           else if (valu1 == '(') valu1 = eval(LPREN);
  96.                           else if (valu1 == HIGH) valu1 = eval(UOP3) >> 8;
  97.                           else if (valu1 == LOW) valu1 = eval(UOP3) & 0xff;
  98.                           else if (valu1 == '*') valu1 = pc;
  99.                           else { evalerr = TRUE;  markerr('E');  break; }
  100.  
  101.             case VALUE:   for (;;) {
  102.                               switch (getitem(&valu2,SMALST)) {
  103.                                   case REGISTR:
  104.                                   case IMMED:   evalerr = TRUE;  markerr('S');
  105.                                                 break;
  106.  
  107.                                   case COMMA:   if (prec != START) backitem(0,COMMA);
  108.                                                 goto endeval;
  109.  
  110.                                   case END_LIN: backitem(0,END_LIN);
  111.                                   endeval:      if (prec == LPREN) {
  112.                                                     evalerr = TRUE;
  113.                                                     markerr('(');
  114.                                                 }
  115.                                                 return (evalerr) ? 0 : valu1;
  116.  
  117.                                   case VALUE:   evalerr = TRUE;  markerr('E');
  118.                                                 break;
  119.  
  120.                                   case OPERATR: if (valu2 == '(' || valu2 == NOT || valu2 == HIGH || valu2 == LOW) {
  121.                                                     evalerr = TRUE;
  122.                             markerr('E');
  123.                                                     break;
  124.                                                 }
  125.                                                 if (prec <= getprec(valu2)) {
  126.                             backitem(valu2,OPERATR);
  127.                                                     return valu1;
  128.                                                 }
  129.                                                 switch (c = valu2) {
  130.                                                     case '+':   valu1 += eval(ADDIT); break;
  131.  
  132.                                                     case '-':   valu1 -= eval(ADDIT); break;
  133.  
  134.                                                     case '*':   valu1 *= eval(MULT); break;
  135.  
  136.                                                     case '/':   valu1 /= eval(MULT); break;
  137.  
  138.                                                     case MOD:   valu1 %= eval(MULT); break;
  139.  
  140.                                                     case AND:    valu1 &= eval(LOG1); break;
  141.  
  142.                                                     case OR:    valu1 |= eval(LOG2); break;
  143.  
  144.                                                     case XOR:   valu1 ^= eval(LOG2); break;
  145.  
  146.                                                     case '>':   valu1 = (valu1 > eval(RELAT)) ? 0xffff : 0; break;
  147.  
  148.                                                     case GRTEQ: valu1 = (valu1 >= eval(RELAT)) ? 0xffff : 0; break;
  149.  
  150.                                                     case '=':   valu1 = (valu1 == eval(RELAT)) ? 0xffff : 0; break;
  151.  
  152.                                                     case NOTEQ: valu1 = (valu1 != eval(RELAT)) ? 0xffff : 0; break;
  153.  
  154.                                                     case '<':   valu1 = (valu1 < eval(RELAT)) ? 0xffff : 0; break;
  155.  
  156.                                                     case LESEQ: valu1 = (valu1 <= eval(RELAT)) ? 0xffff : 0; break;
  157.  
  158.                                                     case SHL:   if ((valu2 = eval(MULT)) > 15) {
  159.                                                                     evalerr = TRUE;
  160.                                                                     markerr('E');
  161.                                                                     break;
  162.                                                                 }
  163.                                                                 valu1 = valu1 << valu2;
  164.                                                                 break;
  165.  
  166.                                                     case SHR:   if ((valu2 = eval(MULT)) > 15) {
  167.                                                                     evalerr = TRUE;
  168.                                                                     markerr('E');
  169.                                                                     break;
  170.                                                                 }
  171.                                                                 valu1 = valu1 >> valu2;
  172.                                                                 break;
  173.  
  174.                                                     case ')':   if (prec == LPREN) return valu1;
  175.                                                                 else {
  176.                                                                     evalerr = TRUE;
  177.                                                                     markerr('(');
  178.                                                                     break;
  179.                                                                 }
  180.                                                 }
  181.                               }
  182.                           }
  183.         }
  184.     }
  185. }
  186.  
  187. /*
  188. Function to get the p